package com.ting;

import com.ting.mapper.UserMapper;
import com.ting.model.User;
import com.ting.mybatis.io.MyResource;
import com.ting.mybatis.session.MySqlSession;
import com.ting.mybatis.session.MySqlSessionFactory;
import com.ting.mybatis.session.MySqlSessionFactoryBuilder;

import java.io.InputStream;
import java.nio.file.attribute.UserDefinedFileAttributeView;

public class Test {
    public static void main(String[] args) {

        //第一步：读取mybatis-config配置文件
        String resource="mybatis-config";
        InputStream inputStream = MyResource.getResourceAsStream(resource);

        //第二步：构建SqlSessionFactory
        MySqlSessionFactory sqlSessionFactory=new MySqlSessionFactoryBuilder().build(inputStream);

        //第三步：打开SqlSessionFactory中获取SqlSession
        MySqlSession session = sqlSessionFactory.openSession();

        //第四步：获取mapper接口对象,这是个动态代理对象
        UserMapper userMapper=session.getMapper(UserMapper.class);

        //第五步：调用mapper接口对象的方法操作数据库
        //当调用动态代理对象的方法时，会被动态代理对象（也就是实现了InvocationHandler的类拦截，然后实现invoke方法）拦截
        User user=userMapper.selectByPrimaryKey(1);

    }
}
